import json
import os
from datetime import datetime


class TutorialPipeline:
    def __init__(self):
        self.first_item = None
        self.file = None
        self.filename = None

    def open_spider(self, spider):
        # 创建输出目录
        if not os.path.exists('output'):
            os.makedirs('output')

        # 创建带时间戳的输出文件
        self.filename = f'output/products.json'
        self.file = open(self.filename, 'w', encoding='utf-8')
        self.file.write('[\n')
        self.first_item = True

    def close_spider(self, spider):
        self.file.write('\n]')
        self.file.close()
        spider.logger.info(f'数据已保存到 {self.filename}')

    def process_item(self, item, spider):
        # 将数据写入JSON文件
        line = json.dumps(dict(item), ensure_ascii=False)
        if self.first_item:
            self.file.write(line)
            self.first_item = False
        else:
            self.file.write(',\n' + line)
        return item